package org.xtremeobjects.forms.ui.editors;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.xtremeobjects.domain.core.di.Injector;
import org.xtremeobjects.domain.model.Clazz;
import org.xtremeobjects.domain.model.Property;
import org.xtremeobjects.forms.ui.XObjectEditorInput;
import org.xtremeobjects.forms.ui.XObjectEditorInputFactory;
import org.xtremeobjects.forms.ui.actions.OpenObjectEditorAction;

public class CollectionTable extends Composite {

	private Table table = null;
	private TableViewer tableViewer = null;
	private FormToolkit formToolkit = null; // @jve:decl-index=0:visual-constraint="45,363"
	private IWorkbenchWindow window = null;
	private Clazz model = null;
	private List collection = null;
	
	CollectionTable(Clazz model, List collection, Composite parent, IWorkbenchWindow window) {
		super(parent, SWT.NONE);
		this.model = model;
		this.collection = collection;
		this.window = window;
		setLayout(new GridLayout());
		createTable();
		createTableCollection();
		updateInput();
	}

	private void createTable() {
		table = getFormToolkit().createTable(this,
				SWT.VIRTUAL | SWT.FULL_SELECTION | SWT.MULTI);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		tableViewer = new TableViewer(table);
		tableViewer.addDoubleClickListener(
				new IDoubleClickListener() {
					public void doubleClick(DoubleClickEvent event) {
						System.out.println(event);
						TableItem[] items = table.getSelection();
						List objs = new ArrayList();
						for (TableItem item : items) {
							Object data = item.getData();
							if (data != null) {
								objs.add(data);
								OpenObjectEditorAction action = new OpenObjectEditorAction();
								action.init( window );
								XObjectEditorInputFactory factory = (XObjectEditorInputFactory) Injector.getInstance(XObjectEditorInputFactory.class);
								action.run( factory.create(data) );
								
							}
						}
					}
				});
	}
	
	private void createTableCollection() {

		ObservableListContentProvider contentProvider = new ObservableListContentProvider();
		tableViewer.setContentProvider(contentProvider);

		configureTableColumns(getProperties());
		createLabelProvidersForProperties(contentProvider, getPropertiesNames());

//		updateInput();
	}

	private String[] getPropertiesNames() {
		return model.getPropertiesNames();
	}

	private void createLabelProvidersForProperties(ObservableListContentProvider contentProvider, String[] propertiesNames) {
		tableViewer.setLabelProvider(new ITableLabelProvider() {
			

			public Image getColumnImage(Object element, int columnIndex) {
				// TODO Auto-generated method stub
				return null;
			}

			public String getColumnText(Object element, int columnIndex) {
				Object obj = getProperties().get(columnIndex).getValue(element); 
				if(obj==null)
					return "";
				return obj.toString();
			}

			public void addListener(ILabelProviderListener listener) {
				// TODO Auto-generated method stub
				
			}

			public void dispose() {
				// TODO Auto-generated method stub
				
			}

			public boolean isLabelProperty(Object element, String property) {
				// TODO Auto-generated method stub
				return false;
			}

			public void removeListener(ILabelProviderListener listener) {
				// TODO Auto-generated method stub
				
			} });
	}

	protected List<Property> getProperties() {
		return model.getProperties();
	}

	private void configureTableColumns(List<Property> properties) {
		for (Property property : properties) {
			if(!property.isHidden())
				createTableColumn(property);
		}
	}

	private void createTableColumn(Property property) {
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setText(property.getLabel());
		tableColumn.setData(property);
		tableColumn.setWidth(120);
	}
	
	
	
	/**
	 * This method initializes formToolkit
	 *
	 * @return org.eclipse.ui.forms.widgets.FormToolkit
	 */
	private FormToolkit getFormToolkit() {
		if (formToolkit == null) {
			formToolkit = new FormToolkit(Display.getCurrent());
		}
		return formToolkit;
	}

	public void setInput(WritableList exampleData) {
		tableViewer.setInput(exampleData);
	}

	private WritableList exampleData = null;
	
	
	public void updateInput() {
		List collection = getCollection();
		exampleData = new WritableList(realm, collection, getImplementationClass());
		setInput(exampleData);
	}

	private Class getImplementationClass() {
		return model.getImplementationClass();
	}

	private List getCollection() {
		return collection;
	}

	private static Realm realm = SWTObservables.getRealm(Display.getDefault());

	
}
